package com.example.cinema.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cinema.R;
import com.example.cinema.drawview.BufferProgress;
import com.example.cinema.drawview.ShortPlayerVideo;
import com.example.cinema.drawview.RoundImageStork;

import java.util.List;

/**
 * 短视频滑动适配器
 * @param <T>
 */
public class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerItemView> {

    private List<T> data;
    private int layoutid;
    private Context context;

    //构造方法传入需要的数据源 布局ID 上下
    public RecyclerAdapter(List<T> data, int layoutid, Context context) {
        this.data = data;
        this.layoutid = layoutid;
        this.context = context;

    }

    @NonNull
    @Override
    public RecyclerItemView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //通过DataBindingUtil.inflate 获取ViewDataBinding 对象
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), layoutid, parent, false);
        binding.getRoot().findViewById(R.id.follow).setVisibility(View.VISIBLE);
        //创建Recycler视图关联类减DataBinding传递过去
        RecyclerItemView itemView = new RecyclerItemView(binding);
        return itemView;
    }


    private ViewDataBinding dataBinding;
    public ShortPlayerVideo videoplay;
    public LinearLayout comment;
    public LinearLayout share;
    private RoundImageStork roundImageStork;
    private TextView vipvideo;
    private BufferProgress bufferProgress;
    private ImageView follow;

    @Override
    public void onBindViewHolder(@NonNull RecyclerItemView holder, int position) {
        Log.i("AAA", position + "当前");
        dataBinding = holder.dataBinding;
        holder.dataBinding.setVariable(BR.mydata, data.get(position));

        this.videoplay = dataBinding.getRoot().findViewById(R.id.player);
        this.comment = dataBinding.getRoot().findViewById(R.id.comment);
        this.share = dataBinding.getRoot().findViewById(R.id.share);
        this.roundImageStork = dataBinding.getRoot().findViewById(R.id.roundImage);
        this.vipvideo = dataBinding.getRoot().findViewById(R.id.vipvideo);
        this.bufferProgress = dataBinding.getRoot().findViewById(R.id.progress);
        this.bufferProgress.setVisibility(View.VISIBLE);
        this.follow = dataBinding.getRoot().findViewById(R.id.follow);
        //评论点击
        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("KKKK", "评论点击");
                if (shareclick != null) {
                    commentclick.CommentClick();
                }
            }
        });
        //分享点击
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("KKKK", "分享点击");
                if (commentclick != null) {

                    shareclick.ShareClick();
                }
            }
        });
        //头像点击
        roundImageStork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userImgClickListener != null) {
                    userImgClickListener.UserImgClick();
                }
            }
        });
        //点击观看完整版
        vipvideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vipclickListener != null) {
                    vipclickListener.VipClick();
                }
            }
        });
        //视频缓冲回调
        videoplay.setOnLoadingListener(new ShortPlayerVideo.OnloadingListener() {
            @Override
            public void loading() {
                bufferProgress.setVisibility(View.VISIBLE);
                bufferProgress.Animators();
            }
        });
        //缓冲动画结束回调
        bufferProgress.setOnCompleteListener(new BufferProgress.OnCompleteListener() {
            @Override
            public void Complete() {

                bufferProgress.setVisibility(View.INVISIBLE);
            }
        });
        //关注点击
        follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("AAAA", "点击有效");
                if(onFollowClickListener!=null)
                {
                    onFollowClickListener.FollowClick(v,data.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    //点击观看完整版
    public interface onVipVideoClickListener {
        void VipClick();
    }

    //分享点击接口
    public interface onShareClickListener {
        void ShareClick();
    }

    //评论点击接口
    public interface onCommentClickListener {
        void CommentClick();
    }

    //头像点击接口
    public interface onUserImgClickListener {
        void UserImgClick();
    }

    //关注接口
    public interface onFollowClickListener {
        void FollowClick(View view,Object obj);
    }

    public onShareClickListener shareclick;
    public onCommentClickListener commentclick;
    public onVipVideoClickListener vipclickListener;
    public onUserImgClickListener userImgClickListener;
    public onFollowClickListener onFollowClickListener;

    public void setShareClick(onShareClickListener shareClick) {
        this.shareclick = shareClick;
    }

    public void setCommentclick(onCommentClickListener commentclick) {
        this.commentclick = commentclick;
    }

    public void setVipClick(onVipVideoClickListener clickListener) {
        this.vipclickListener = clickListener;
    }

    public void setUserClick(onUserImgClickListener clickListener) {
        this.userImgClickListener = clickListener;
    }

    public void setFollowClick(onFollowClickListener followClick){
        this.onFollowClickListener = followClick;
    }
}

class RecyclerItemView extends RecyclerView.ViewHolder {
    public ViewDataBinding dataBinding;

    //将传递的视图改为ViewDataBinding
    public RecyclerItemView(@NonNull ViewDataBinding itemView) {
        super(itemView.getRoot());//从ViewDataBinding中获取View返回给开继承类
        this.dataBinding = itemView;//将ViewDataBinding 复制给变量 后续待用
    }

}
